//模拟翻牌
int LEDS[] = {3,5,7};
int BUTTONS[] = {2,4,6};

unsigned long gameStartTime;
const unsigned long gameDuration = 1500;
long lastBounceTime = 0;
const long debounceDelay = 1000;
const int max = 5;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
  for(int i = 0;i<3;i++){
    pinMode(LEDS[i],OUTPUT);
    pinMode(BUTTONS[i],INPUT_PULLUP);
  }
}

void loop() {
  // put your main code here, to run repeatedly
int score = 0;
int cnt = 0;
int leds[max] = {0};
bool gameStarted = false;
if(!gameStarted&&digitalRead(BUTTONS[0])==LOW&&digitalRead(BUTTONS[1])==LOW&&digitalRead(BUTTONS[2])==LOW){
  Serial.println("游戏开始");
  gameStarted = true;
  score = 0;
  delay(1000);
}
if(gameStarted){
  while(cnt<max){ 
    int led = random(0,3);
    leds[cnt] = led;
    digitalWrite(LEDS[led],HIGH);
    delay(50);
    digitalWrite(LEDS[led],LOW);
    cnt++;
    delay(300);
  } 
   delay(1000);
  Serial.println("请开始按键");
  gameStartTime = millis();
  while(millis()-gameStartTime<gameDuration){
    for(int cnt = 0;cnt<max;cnt++){
      while(digitalRead(BUTTONS[0])==HIGH&&digitalRead(BUTTONS[1])==HIGH&&digitalRead(BUTTONS[2])==HIGH){}
      lastBounceTime = millis();
        delay(100);
        if(digitalRead(BUTTONS[leds[cnt]])==LOW){
          if(millis()-lastBounceTime<debounceDelay){
            delay(100);
            Serial.println("得分+1");
            digitalWrite(LEDS[leds[cnt]],HIGH);
            delay(50);
            digitalWrite(LEDS[leds[cnt]],LOW);
          score++;
          }
        }else{
            Serial.println("按错了，不算分，游戏结束");
            goto next;
          }
    }
  }next:
    Serial.println("灯亮起的顺序是：");
  for(int cnt =0;cnt<max;cnt++){
    Serial.print(leds[cnt]);
  }
  Serial.println();
  Serial.println("你的得分是：");
  Serial.println(score);
  delay(3000);
  }
}
 





